package cn.tedu.ivos.geofence.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceSaveParam;
import cn.tedu.ivos.geofence.pojo.entity.Geofence;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    private GeofenceMapper geofenceMapper;
    @Autowired
    private VehicleMapper vehicleMapper;

    @Override
    public List<GeofenceVO> selectGeofence(GeofenceQuery geofenceQuery) {
        List<GeofenceVO> geofenceVOS = geofenceMapper.selectGeofence(geofenceQuery);
        //补充围栏需要的总车辆和可用车辆
        for (GeofenceVO geofenceVO : geofenceVOS) {
            VehicleQuery vehicleQuery = new VehicleQuery();
            //查询当前围栏中的所有车辆
            vehicleQuery.setGeofenceId(geofenceVO.getId());
            List<VehicleVO> vehicleVOS = vehicleMapper.selectVehicle(vehicleQuery);
            //获取当前围栏的总车辆
            int totalNum = 0;
            if(vehicleVOS!=null && vehicleVOS.size()>0) {
                totalNum = vehicleVOS.size();
            }
            //可用车辆
            int availableNum = 0;
            for (VehicleVO vehicleVO : vehicleVOS) {
                if(vehicleVO.getStatus().equals("1"))
                    availableNum++;
            }
            //给当前围栏进行赋值
            if(availableNum>0){
                geofenceVO.setAvailableNum(availableNum);
            }else{
                geofenceVO.setAvailableNum(0);
            }
            geofenceVO.setTotalNum(totalNum);
            geofenceVO.setVehicleList(vehicleVOS);
        }
        return geofenceVOS;
    }

    @Override
    public void saveGeofence(GeofenceSaveParam geofenceSaveParam) {
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geofenceSaveParam,geofence);
        //前端数据的position对象是HashMap类型的，geofence的position是String类型的，需要转化赋值
        HashMap<String,String> position = geofenceSaveParam.getPosition();
        //准备ObjectMapper，JSON处理库
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String positionStr = objectMapper.writeValueAsString(position);
            //geofenceId为null，就是新增，如果不为null，就是修改
            if(geofenceSaveParam.getId()==null) {
                geofence.setPosition(positionStr);
                geofence.setStatus("1");
                geofence.setCreateTime(new Date());
                geofenceMapper.insert(geofence);
            }else{
                geofenceMapper.update(geofence);
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void deleteGeofence(Long id) {
        //删除前需要检查该围栏中是否有车辆
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(id);
        List<VehicleVO> vehicleVOS = vehicleMapper.selectVehicle(vehicleQuery);
        if(vehicleVOS!=null && vehicleVOS.size()>0){
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }else {
            //没有车辆才可以删除围栏信息
            geofenceMapper.deleteById(id);
        }
    }
}
